package com.sugar.queryinfo.module.business.controller.admin.data.dataquerylsbg;

import com.sugar.queryinfo.module.business.controller.admin.data.dataquerylsbg.vo.DataQueryLsbgPageReqVO;
import com.sugar.queryinfo.module.business.controller.admin.data.dataquerylsbg.vo.DataQueryLsbgRespVO;
import com.sugar.queryinfo.module.business.controller.admin.data.dataquerylsbg.vo.DataQueryLsbgSaveReqVO;
import com.sugar.queryinfo.module.business.util.HideUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.queryinfo.framework.common.pojo.PageParam;
import com.sugar.queryinfo.framework.common.pojo.PageResult;
import com.sugar.queryinfo.framework.common.pojo.CommonResult;
import com.sugar.queryinfo.framework.common.util.object.BeanUtils;
import static com.sugar.queryinfo.framework.common.pojo.CommonResult.success;

import com.sugar.queryinfo.framework.excel.core.util.ExcelUtils;

import com.sugar.queryinfo.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.queryinfo.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.queryinfo.module.business.dal.dataobject.dataquerylsbg.DataQueryLsbgDO;
import com.sugar.queryinfo.module.business.service.dataquerylsbg.DataQueryLsbgService;

@Tag(name = "管理后台 - 数据查询-流水报告")
@RestController
@RequestMapping("/business/data-query-lsbg")
@Validated
public class DataQueryLsbgController {

    @Resource
    private DataQueryLsbgService dataQueryLsbgService;

    @PostMapping("/create")
    @Operation(summary = "创建数据查询-流水报告")
    @PreAuthorize("@ss.hasPermission('business:data-query-lsbg:create')")
    public CommonResult<Long> createDataQueryLsbg(@Valid @RequestBody DataQueryLsbgSaveReqVO createReqVO) {
        return success(dataQueryLsbgService.createDataQueryLsbg(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新数据查询-流水报告")
    @PreAuthorize("@ss.hasPermission('business:data-query-lsbg:update')")
    public CommonResult<Boolean> updateDataQueryLsbg(@Valid @RequestBody DataQueryLsbgSaveReqVO updateReqVO) {
        dataQueryLsbgService.updateDataQueryLsbg(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除数据查询-流水报告")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:data-query-lsbg:delete')")
    public CommonResult<Boolean> deleteDataQueryLsbg(@RequestParam("id") Long id) {
        dataQueryLsbgService.deleteDataQueryLsbg(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除数据查询-流水报告")
                @PreAuthorize("@ss.hasPermission('business:data-query-lsbg:delete')")
    public CommonResult<Boolean> deleteDataQueryLsbgList(@RequestParam("ids") List<Long> ids) {
        dataQueryLsbgService.deleteDataQueryLsbgListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得数据查询-流水报告")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:data-query-lsbg:query')")
    public CommonResult<DataQueryLsbgRespVO> getDataQueryLsbg(@RequestParam("id") Long id) {
        DataQueryLsbgDO dataQueryLsbg = dataQueryLsbgService.getDataQueryLsbg(id);
        return success(BeanUtils.toBean(dataQueryLsbg, DataQueryLsbgRespVO.class, respVO ->
                respVO.setQueryName(HideUtil.name(respVO.getQueryName()))
                        .setQueryIdCard(HideUtil.idCard(respVO.getQueryIdCard()))));
    }

    @GetMapping("/page")
    @Operation(summary = "获得数据查询-流水报告分页")
    @PreAuthorize("@ss.hasPermission('business:data-query-lsbg:query')")
    public CommonResult<PageResult<DataQueryLsbgRespVO>> getDataQueryLsbgPage(@Valid DataQueryLsbgPageReqVO pageReqVO) {
        PageResult<DataQueryLsbgDO> pageResult = dataQueryLsbgService.getDataQueryLsbgPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DataQueryLsbgRespVO.class, respVO ->
                respVO.setQueryName(HideUtil.name(respVO.getQueryName()))
                        .setQueryIdCard(HideUtil.idCard(respVO.getQueryIdCard()))));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出数据查询-流水报告 Excel")
    @PreAuthorize("@ss.hasPermission('business:data-query-lsbg:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDataQueryLsbgExcel(@Valid DataQueryLsbgPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DataQueryLsbgDO> list = dataQueryLsbgService.getDataQueryLsbgPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "数据查询-流水报告.xls", "数据", DataQueryLsbgRespVO.class,
                        BeanUtils.toBean(list, DataQueryLsbgRespVO.class));
    }

}